package org.hawk.gwt.ppc.parser.impl;

import java.util.ArrayList;
import java.util.List;

import org.hawk.gwt.ppc.parser.BlockDescriptor;
import org.hawk.gwt.ppc.parser.Descriptor;
import org.hawk.gwt.ppc.parser.ExpressionDescriptor;
import org.hawk.gwt.ppc.parser.Token;
import org.hawk.gwt.ppc.utils.PreprocessorException;
import static org.hawk.gwt.ppc.parser.impl.DescriptorParserUtils.*;

/**
 * Parses block of code (between { and } brackets).
 * Note that modifiers (if any) should be passed to this parser as parameters.
 * 
 * @author alex.bereznevatiy@gmail.com
 *
 */
final class BlockDescriptorParser implements DescriptorParser {

	/*
	 * (non-Javadoc)
	 * @see org.hawk.gwt.ppc.parser.impl.DescriptorParser#accepts(org.hawk.gwt.ppc.parser.Token)
	 */
	public boolean accepts(Token token) {
		return token.equalsTo('{');
	}

	/*
	 * (non-Javadoc)
	 * @see org.hawk.gwt.ppc.parser.impl.DescriptorParser#parse(org.hawk.gwt.ppc.parser.impl.TokenBasedJavaParser, org.hawk.gwt.ppc.parser.impl.Tokenizer, java.lang.Object[])
	 */
	public Descriptor parse(TokenBasedJavaParser parser, Tokenizer tokenizer, Object...params)
			throws PreprocessorException {
		// TODO: implement
		int deep = 1;
		while (deep > 0) {
			lookupNext(tokenizer, false);
			Token tok = tokenizer.current();
			if (tok.equalsTo('{')) {
				deep++;
			} else if (tok.equalsTo('}')) {
				deep --;
			}
		}
		lookupNext(tokenizer, false);
		return new BlockDescriptorImpl(new ArrayList<Descriptor>());
	}

	private static class BlockDescriptorImpl extends AbstractDescriptorContainer implements BlockDescriptor {

		BlockDescriptorImpl(List<Descriptor> descriptors) {
			super(descriptors);
		}

		public List<String> getModifiers() {
			// TODO Auto-generated method stub
			return null;
		}

		public List<String> getLabels() {
			// TODO Auto-generated method stub
			return null;
		}
		
		/*
		 * (non-Javadoc)
		 * @see org.hawk.gwt.ppc.parser.impl.AbstractDescriptor#getParent()
		 */
		public ExpressionDescriptor getParent() {
			Descriptor parent = super.getParent();
			if (parent instanceof ExpressionDescriptor) {
				return (ExpressionDescriptor) parent;
			}
			return null;
		}
	}
}
